{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-22T08:25:45.382595Z",
     "start_time": "2025-09-22T08:25:41.604799Z"
    }
   },
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample2.html')\n",
    "\n",
    "# 切换frame\n",
    "wd.switch_to.frame('frame1')\n",
    "elements = wd.find_elements(By.CLASS_NAME, 'plant')\n",
    "\n",
    "for element in elements:\n",
    "    print(element.text)"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n",
      "洋葱\n",
      "白菜\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T09:08:43.970285Z",
     "start_time": "2025-09-22T09:08:25.030917Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample2.html')\n",
    "\n",
    "\n",
    "# 先根据name属性值 'innerFrame'，切换到iframe中\n",
    "wd.switch_to.frame('innerFrame')\n",
    "\n",
    "# 根据 class name 选择元素，返回的是 一个列表\n",
    "elements = wd.find_elements(By.CLASS_NAME, 'plant')\n",
    "\n",
    "for element in elements:\n",
    "    print(element.text)\n",
    "\n",
    "# 切换回 最外部的 HTML 中\n",
    "wd.switch_to.default_content()\n",
    "\n",
    "# 然后再 选择操作 外部的 HTML 中 的元素\n",
    "wd.find_element(By.CSS_SELECTOR, '#outerbutton').click()\n",
    "\n",
    "wd.quit()"
   ],
   "id": "cef13de086f78d44",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n",
      "洋葱\n",
      "白菜\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 切换到新的窗口",
   "id": "b648ec48a9962c9e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T09:09:45.206650Z",
     "start_time": "2025-09-22T09:09:41.128444Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample3.html')\n",
    "\n",
    "# 点击打开新窗口的链接\n",
    "link = wd.find_element(By.TAG_NAME, \"a\")\n",
    "link.click()\n",
    "\n",
    "# wd.title属性是当前窗口的标题栏 文本\n",
    "print(wd.title)"
   ],
   "id": "8ac9311bee94041d",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "白月黑羽测试网页3\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T09:13:48.323528Z",
     "start_time": "2025-09-22T09:13:39.406876Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample3.html')\n",
    "main_window = wd.current_window_handle\n",
    "\n",
    "# 点击打开新窗口的链接\n",
    "link = wd.find_element(By.TAG_NAME, \"a\")\n",
    "link.click()\n",
    "\n",
    "print(wd.window_handles)\n",
    "\n",
    "for handle in wd.window_handles:\n",
    "    wd.switch_to.window(handle)\n",
    "    print(wd.title)\n",
    "\n",
    "wd.quit()"
   ],
   "id": "8e3ca4486273bc2b",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['19ED9F58E029892A0E1DAD4102DF54F6', '7B3A89F0453D3F55262C88426282A0C5']\n",
      "白月黑羽测试网页3\n",
      "搜索 - Microsoft 必应\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "adabf0666f161197"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
